﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CreateEditTargetForm.cs" company="Elilink">
//   Defor
// </copyright>
// <summary>
//   Form for creating new target
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Deployment
{
    using System;
    using System.Windows.Forms;

    using BusinessLayer.Objects;
    using BusinessLayer.Results;

    using DataAccessLayer.Repositories.Abstract;

    using Deployment.Tools;

    /// <summary>
    ///     Form for creating new target
    /// </summary>
    internal partial class CreateEditTargetForm : Form
    {
        #region Fields

        /// <summary>
        ///     The _current system
        /// </summary>
        private readonly TargetSystem _currentSystem;

        /// <summary>
        ///     The _is edit action
        /// </summary>
        private readonly bool _isEditAction;

        /// <summary>
        ///     The _target system repository
        /// </summary>
        private readonly ITargetSystemRepository _targetSystemRepository;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="CreateEditTargetForm" /> class.
        /// </summary>
        public CreateEditTargetForm()
        {
            InitializeComponent();
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="CreateEditTargetForm" /> class.
        /// </summary>
        /// <param name="targetSystemRepository">
        ///     The target system repository.
        /// </param>
        public CreateEditTargetForm(ITargetSystemRepository targetSystemRepository)
            : this()
        {
            _targetSystemRepository = targetSystemRepository;
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="CreateEditTargetForm" /> class.
        /// </summary>
        /// <param name="targetSystemRepository">
        ///     The target system repository.
        /// </param>
        /// <param name="targetSystem">
        ///     The target system.
        /// </param>
        public CreateEditTargetForm(ITargetSystemRepository targetSystemRepository, TargetSystem targetSystem)
            : this(targetSystemRepository)
        {
            _currentSystem = targetSystem;
            _isEditAction = true;
            txtTargetName.Text = _currentSystem.TargetSystemName;
            txtBuildConfiguration.Text = _currentSystem.BuildConfiguration;
        }

        #endregion

        #region Methods

        /// <summary>
        ///     Shows the create dialog.
        /// </summary>
        /// <param name="owner">
        ///     The owner.
        /// </param>
        /// <returns>
        ///     The <see cref="OperationResult" />.
        /// </returns>
        internal OperationResult ShowCreateDialog(Form owner)
        {
            CenterToParent(owner);
            Text = "Create new target";
            switch (ShowDialog(owner))
            {
                case DialogResult.OK:
                    {
                        return new OperationResult(true);
                    }

                default:
                    {
                        return new OperationResult(false);
                    }
            }
        }

        /// <summary>
        ///     Shows the edit dialog.
        /// </summary>
        /// <param name="owner">
        ///     The owner.
        /// </param>
        /// <returns>
        ///     The <see cref="OperationResult" />.
        /// </returns>
        internal OperationResult ShowEditDialog(Form owner)
        {
            CenterToParent(owner);
            Text = "Edit target";
            switch (ShowDialog(owner))
            {
                case DialogResult.OK:
                    {
                        return new OperationResult(txtTargetName.Text);
                    }

                default:
                    {
                        return new OperationResult();
                    }
            }
        }

        /// <summary>
        ///     Handles the Click event of the BtnCancel control.
        /// </summary>
        /// <param name="sender">
        ///     The source of the event.
        /// </param>
        /// <param name="e">
        ///     The <see cref="EventArgs" /> instance containing the event data.
        /// </param>
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        /// <summary>
        ///     Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">
        ///     The source of the event.
        /// </param>
        /// <param name="e">
        ///     The <see cref="EventArgs" /> instance containing the event data.
        /// </param>
        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (!CheckInputEmptiness())
            {
                return;
            }

            if (!PerformAction(_isEditAction))
            {
                return;
            }

            DialogResult = DialogResult.OK;
            Close();
        }

        /// <summary>
        ///     Centers to parent.
        /// </summary>
        /// <param name="owner">
        ///     The owner.
        /// </param>
        private void CenterToParent(Form owner)
        {
            Owner = owner;
            CenterToParent();
        }

        /// <summary>
        ///     Checks the input emptiness.
        /// </summary>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        private bool CheckInputEmptiness()
        {
            if (string.IsNullOrEmpty(txtTargetName.Text))
            {
                MessageBoxEx.Show(
                    this, 
                    "Target name should be specified", 
                    "Fill all data", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Error);
                return false;
            }

            if (string.IsNullOrEmpty(txtBuildConfiguration.Text))
            {
                MessageBoxEx.Show(
                    this, 
                    "Build configuration should be specified", 
                    "Fill all data", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        /// <summary>
        ///     Performs the action.
        /// </summary>
        /// <param name="isEdit">
        ///     if set to <c>true</c> [is edit].
        /// </param>
        /// <returns>
        ///     <c>true</c> if XXXX, <c>false</c> otherwise
        /// </returns>
        private bool PerformAction(bool isEdit)
        {
            var target = new TargetSystem
                             {
                                 BuildConfiguration = txtBuildConfiguration.Text, 
                                 TargetSystemName = txtTargetName.Text
                             };
            OperationResult result = isEdit
                                         ? _targetSystemRepository.UpdateTargetSystemWithName(
                                             target, 
                                             _currentSystem.TargetSystemName)
                                         : _targetSystemRepository.AddTargetSystem(target);

            if (result.ErrorType != 0)
            {
                MessageBoxEx.Show(
                    this, 
                    result.ErrorDescription, 
                    result.ErrorType.ToString(), 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        #endregion
    }
}